package org.infodavid.common.model.impl;

import java.util.Arrays;

import org.infodavid.common.model.IImageData;
import org.infodavid.common.model.impl.AbstractBasicDataObjectImpl;

/**
 * The Class ImageDataImpl.
 */
public class ImageDataImpl extends AbstractBasicDataObjectImpl implements IImageData {

	/** serialVersionUID. */
	private static final long serialVersionUID = 8567730969539674514L;

	/** The type. */
	private IImageData.EImageType type;

	/** The value. */
	private byte[] value;

	/** The name. */
	private String name;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected ImageDataImpl() throws Exception {
		super();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IImageData#getValue()
	 */
	public byte[] getValue() {
		return value;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IImageData#setValue(byte[])
	 */
	public void setValue(final byte[] arg0) {
		if (!equals(value, arg0)) {
			value = arg0;
			markModified(VALUE);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.IImageData#getType()
	 */
	public IImageData.EImageType getType() {
		return type;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.model.IImageData#setType(org.infodavid.common.model.IImageData.EImageType)
	 */
	public void setType(final IImageData.EImageType arg0) {
		if (!equals(type, arg0)) {
			type = arg0;
			markModified(TYPE);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IBasicDataObject#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		type = null;
		value = null;
		name = null;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (getValue() == null ? 0 : Arrays.hashCode(getValue()));

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IImageData)) {
			return false;
		}

		final IImageData other = (IImageData)obj;

		if (getValue() == null) {
			if (other.getValue() != null) {
				return false;
			}
		}
		else if (!Arrays.equals(getValue(), other.getValue())) {
			return false;
		}

		return true;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append(", type=");
		builder.append(type);
		builder.append(", name=");
		builder.append(name);
		builder.append(", value=");

		if (value == null) {
			builder.append("null");
		}
		else {
			builder.append(value.length);
			builder.append(" bytes");
		}

		return builder;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#getName()
	 */
	public String getName() {
		return name;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#setName(java.lang.String)
	 */
	public void setName(final String value) {
		if (!equals(name, value)) {
			name = value;
			markModified(NAME);
		}
	}
}
